package com.wis.service.impl;

import com.wis.entity.CourseEntity;
import com.wis.mapper.CourseMapper;
import com.wis.service.CourseService;
import com.wis.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public CommonResult selectCourseByName(String name,  int page, int limit) {
        List<CourseEntity> courseEntityList = courseMapper.findBlurry(name,limit,page-1);
        return CommonResult.build(200, "ok", courseEntityList.size(), courseEntityList);
    }

    @Override
    public CommonResult delCourse(Integer id) {
        return courseMapper.delCourse(id) > 0 ? CommonResult.ok() : CommonResult.fail();
    }

    @Override
    public CommonResult updateCourse(CourseEntity course) {
        // 调用Mapper更新数据
        int i = courseMapper.updateCourse(course);
        if (i > 0) {  // 更新成功
            return CommonResult.build(200, "ok", i,course);
        }
        // 更新失败
        return CommonResult.build(500, "no", i, course);
    }

    @Override
    public CommonResult addCourse(CourseEntity course) {
        // 调用Mapper插入数据
        int i = courseMapper.addCourse(course);
        if (i > 0) {  // 插入成功
            return CommonResult.build(200, "ok", i,course);
        }
        // 插入失败
        return CommonResult.build(500, "no", i, course);
    }
}
